from flask import Blueprint, jsonify

from extension import db
from models.animal import Animal

bp = Blueprint('animals', __name__)


@bp.route('/animals', methods=['GET'])
def get_animals():
    animals = Animal.query.all()
    return jsonify([a.to_dict() for a in animals])


@bp.route('/animals/<int:id>', methods=['GET'])
def get_animal(id):
    animal = Animal.query.get_or_404(id)
    return jsonify(animal.to_dict())


@bp.route('/animals/<int:id>/praise', methods=['POST'])
def praise_animal(id):
    animal = Animal.query.get_or_404(id)
    animal.praise += 1
    db.session.commit()
    return jsonify({'message': 'Praised successfully', 'praise_count': animal.praise}), 200
